package com.system.borrow.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.system.borrow.Constant.URLDefind;
import com.system.borrow.R;
import com.system.borrow.adapter.LookAdapter;
import com.system.borrow.adapter.LookBean;
import com.system.borrow.bean.TuiJian;
import com.system.borrow.callback.DialogCallback;
import com.system.borrow.http.utils.OkHttpUtils;
import com.system.borrow.utils.SecondTitle;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import okhttp3.Request;
import okhttp3.Response;

/**
 * 浏览记录
 */
public class LookActivity extends BaseActivity {
    ListView lv_data;
    LookAdapter adapter;
    LinearLayout ll_nodata;
    TextView tv_loan;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look);
        SecondTitle title = new SecondTitle(this);
        title.setTitle("浏览记录",null);
        initView();
        addAction();
        loadData();
        initListner();
    }

    private void initListner() {
        lv_data.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TuiJian action = (TuiJian) adapter.getItem(position);
                if(action==null){
                    return;
                }
                Intent intent = new Intent(LookActivity.this, WebViewActivity.class);
                intent.putExtra("name",action.name);
                intent.putExtra("url",action.url);
                intent.putExtra("id",action.id);
                intent.putExtra("type",1);
                startActivity(intent);
            }
        });
        tv_loan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent inten = new Intent(LookActivity.this,LoanActivity.class);
                startActivity(inten);
            }
        });
    }

    private void loadData() {
        OkHttpUtils.get(URLDefind.LOOK_LIST)
                .tag(this)
                .execute(new DialogCallback<String>(this,String.class,"加载中...") {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        if(s!=null){
                            try {
                                JSONObject json = new JSONObject(s);
                                int statusCode = json.getInt("statusCode");
                                if(statusCode==200){
                                    JSONArray data = json.getJSONArray("data");
                                    Gson gson = new Gson();
                                    TypeToken<List<LookBean>> token = new TypeToken<List<LookBean>>(){};
                                    List<LookBean> tuiJianList = gson.fromJson(data.toString(),token.getType());
                                    if(tuiJianList.size()>0){
                                        adapter = new LookAdapter(LookActivity.this,tuiJianList);
                                        lv_data.setAdapter(adapter);
                                        lv_data.setVisibility(View.VISIBLE);
                                        ll_nodata.setVisibility(View.GONE);
                                    }else{
                                        lv_data.setVisibility(View.GONE);
                                        ll_nodata.setVisibility(View.VISIBLE);
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onCacheResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {

                    }
                });
    }

    private void addAction() {
    }

    private void initView() {
        lv_data = (ListView) findViewById(R.id.lv_data);
        ll_nodata = (LinearLayout) findViewById(R.id.ll_nodata);
        tv_loan = (TextView) findViewById(R.id.tv_loan);
    }
}
